<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-5-11
 * Time: 9:12
 */
namespace app\common\model;

use think\Db;
use think\Model;

/**
 * 技师阶梯提成配置
 * @package app\admin\model
 */
class EngineerConfig extends Model
{
    protected $name = 'qt_engineer_config';
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;


    /**
     * 计算项目阶梯提成
     * @param $data [上钟参数]
     * @return array
     */
    public static function calculateCommission($data){
        //$projectId,$orderType,$projectNumber
        $commissionOriginal = [];
        if(is_array($data)){
            foreach ($data as $key=>$value){
                $find = EngineerConfigItem::where([
                    'project_id'=>$value['project_id'],
                    'clock_type'=>$value['status'],
                    'com_project_id'=>$value['project_id'],
                    'com_clock_type'=>$value['status']
                ])->select()->toArray();
                if(!empty($find)){
                    foreach ($find as $k=>$v){
                        $v['number'] = $value['number'];
                        $commissionOriginal[] = $v;
                    }
                }
            }
        }
        $resultCommission = [];
        //符合要求的数据
        foreach ($commissionOriginal as $key=>$value){
            if (isset($resultCommission[$value['config_id']])) {
                $resultCommission[$value['config_id']]['number'] += $value['number'];
            }else {
                $resultCommission[$value['config_id']] = $value;
                $resultCommission[$value['config_id']]['number'] = $value['number'];
            }
        }
        //判断符合要求的数据是不是包含同样的商品和项目，如果有拿最大的
        foreach ($resultCommission as $key=>$value){
            $projectIds = Db::name('qt_engineer_config_item')
                ->where(['config_id'=>$value['config_id']])
                ->group('project_id')
                ->column('project_id');
            $resultCommission[$key]['project_data'] = implode(',',$projectIds);
        }
        $maxData = [];
        foreach ($resultCommission as $key=>$value){
            if (isset($resultCommission[$value['project_data']])) {
                $maxData[$value['project_data']][]  = $value;
            }else {
                $maxData[$value['project_data']][] = $value;
            }
        }
        $resultMaxData = [];
        foreach ($maxData as $key=>$value){
            $max = array_column($resultCommission, 'clock_min',$key);
            $resultMaxData[$key] = $value[key($max)];
        }

        $config= [];//符合条件的提成规则
        foreach ($resultMaxData as $key=>$value){
            $projectNumber = $value['number'];
            if($value['project_number'] != '1'){
                $projectNumber = bcdiv($value['number'],$value['project_number']);
            }
            if($projectNumber >= $value['clock_min']){
                $config[] = $value;
            }
        }
        $result =[];
        if(!empty($config)){
            $result['commission'] = 0;
            $result['add_commission'] = 0;
            foreach ($config as $key=>$value){
                if($value['com_clock_type'] != '1'){
                    $value['money'] = bcmul($value['money'],$value['com_project_number']);
                }
                if($value['money_type'] == '1'){
                    $money = $value['money']*$projectNumber;
                    $result['commission'] += $money;
                }
                if($value['money_type'] == '2'){
                    $result['add_commission'] +=$value['money'];
                }
            }
        }else{
            $result['commission'] = 0;//阶梯提成
            $result['add_commission'] = 0;//额外提成
        }
        return $result;
    }


}
